package cn.fsd.controller;

import cn.fsd.domain.Receipt;
import cn.fsd.domain.Task;
import cn.fsd.exception.AppException;
import cn.fsd.exception.SubStationExceptionCodeMsg;
import cn.fsd.query.substation.PaymentQuery;
import cn.fsd.query.substation.TaskQuery;
import cn.fsd.query.substation.WorkloadQuery;
import cn.fsd.service.impl.TaskServiceImpl;
import cn.fsd.vo.PageBean;
import cn.fsd.vo.ResultBean;
import cn.fsd.query.substation.WorkloadBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/task")
public class TaskController {

  @Autowired
  TaskServiceImpl taskService;

  @PostMapping("/query")
  @ApiOperation(value = "根据日期，任务类型，任务状态查询分站的任务单")
  public ResultBean queryTasksOfStation(@RequestBody TaskQuery query){
    if(query==null||StringUtils.isEmpty(query.getStation_id())){
      throw new AppException(SubStationExceptionCodeMsg.STATION_ID_EMPTY);
    }
    return ResultBean.success(taskService.queryStationAllTasks(query));
  }

  @GetMapping("/{id}")
  @ApiOperation(value = "根据任务单id查询任务单详情信息")
  public ResultBean getTaskInfoById(@PathVariable("id")String id){
    if(StringUtils.isEmpty(id)){
      throw new AppException(SubStationExceptionCodeMsg.TASK_ID_EMPTY);
    }
    return ResultBean.success(taskService.getTaskInfoById(id));
  }

  @PutMapping("/allocateCourier")
  @ApiOperation(value = "给任务单分配配送员并修改任务单状态")
  public ResultBean allocateCourier(@RequestBody Task task){
    if(StringUtils.isEmpty(task.getTask_id())){
      throw new AppException(SubStationExceptionCodeMsg.TASK_ID_EMPTY);
    }
    if(StringUtils.isEmpty(task.getCourier_id())){
      throw new AppException(SubStationExceptionCodeMsg.COURIER_ID_EMPTY);
    }
    taskService.allocateCourier(task);
    return ResultBean.success();
  }

  @PostMapping("/queryDelivery")
  @ApiOperation(value = "查询配送单")
  public ResultBean getDelivery(@RequestBody TaskQuery query){
    if(StringUtils.isEmpty(query.getStation_id())){
      throw new AppException(SubStationExceptionCodeMsg.STATION_ID_EMPTY);
    }
    return ResultBean.success(taskService.getDeliveryPage(query));
  }

  @PostMapping("/queryReceipt")
  @ApiOperation(value = "查询回执单")
  public ResultBean getReceipt(@RequestBody TaskQuery query){
    if(StringUtils.isEmpty(query.getStation_id())){
      throw new AppException(SubStationExceptionCodeMsg.STATION_ID_EMPTY);
    }
    return ResultBean.success(taskService.getReceipts(query));
  }

  @PutMapping("/signGoods")
  @ApiOperation(value = "签收商品,将对应的order和task状态修改为已完成")
  public ResultBean signGoods(@RequestBody Receipt receipt){
    if(StringUtils.isEmpty(receipt.getTask_id())){
      throw new AppException(SubStationExceptionCodeMsg.TASK_ID_EMPTY);
    }
    taskService.signGoods(receipt);
    return ResultBean.success();
  }

  @PostMapping("/queryPayment")
  @ApiOperation("分站缴款记录查询")
  public ResultBean queryStationPayment(@RequestBody PaymentQuery query){
    return ResultBean.success(taskService.queryPayment(query));
  }

  /**
   * 微服务之间调用的接口
   */
  @PostMapping("/queryWorkload")
  @ApiOperation("分站工作量查询")
  public PageBean<WorkloadBean> queryWorkload(@RequestBody WorkloadQuery query){
    return taskService.queryStationWork(query);
  }
}
